<?php
namespace app\api\controller\xiluedu;


use app\common\controller\Api;
use app\common\model\xiluedu\SystemMessage;
use app\common\model\xiluedu\UserMessage;
use app\common\model\xiluedu\UserMessageAccount;

class Message extends Api {

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle (消息概况)
     * @ApiSummary (消息总的统计情况)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.message/summary)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function summary() {
        $user = $this->auth->getUser();
        $user_message_account = UserMessageAccount::where('user_id',$user->id)->find();
        $where = [];
        $userMessage = UserMessage::where('user_id', $user->id)->where($where)
            ->order('id', 'desc')
            ->find();
        $activityMessage = SystemMessage::where('type', SystemMessage::TYPE_ACTIVITY)
            ->order('id', 'desc')
            ->find();
        $systemMessage = SystemMessage::where('type', SystemMessage::TYPE_SYSTEM)
            ->order('id', 'desc')
            ->find();
        $data = [
            'user'=>['total'=>$user_message_account->user_message,'message'=>$userMessage],
            'activity'=>['total'=>$user_message_account->activity_message,'message'=>$activityMessage],
            'system'=>['total'=>$user_message_account->system_message,'message'=>$systemMessage]
        ];
        $this->success('查询成功',$data);
    }

    /**
     * @ApiTitle (个人消息)
     * @ApiSummary (个人消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.message/user_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function user_list() {
        $user  =$this->auth->getUser();
        $UserMessageAccount = UserMessageAccount::get(['user_id'=>$user->id]);
        $where = [];
        if($UserMessageAccount->user_message>0) {
            $UserMessageAccount->save(['user_message'=>0]);
        }
        $list = UserMessage::where('user_id', $user->id)
            ->where($where)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', $list);
    }

    /**
     * @ApiTitle (活动消息)
     * @ApiSummary (活动消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.message/activity_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function activity_list() {
        $user  =$this->auth->getUser();
        $UserMessageAccount = UserMessageAccount::get(['user_id'=>$user->id]);
        if($UserMessageAccount->activity_message>0) {
            $UserMessageAccount->save(['activity_message'=>0]);
        }
        $list = SystemMessage::where('type', SystemMessage::TYPE_ACTIVITY)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', $list);
    }

    /**
     * @ApiTitle (系统消息)
     * @ApiSummary (系统消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.message/system_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function system_list() {
        $user  =$this->auth->getUser();
        $UserMessageAccount = UserMessageAccount::get(['user_id'=>$user->id]);
        if($UserMessageAccount->system_message>0) {
            $UserMessageAccount->save(['system_message'=>0]);
        }
        $list = SystemMessage::where('type', SystemMessage::TYPE_SYSTEM)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', $list);
    }

}